package jegpappo.handin3;
import java.util.ArrayList;
import java.util.Arrays;

public class MiningNobel {
	
	private static double[] doubles;
	private static int size = 0;
	
	public static void initArray(String fileName) {
		In input = new In(fileName);
		doubles = new double[size];
		ArrayList<Double> doublesArrayList = new ArrayList<Double>();
		
		for(int i = 0; i < size; i++) {
			String currentLine = input.readLine();
			String[] splitLine = currentLine.split(" , ");
			doubles[i] = Double.parseDouble(splitLine[1]);
		}
	}
	
	public static void findArraySize(String fileName) {
		In input = new In(fileName);
		while(input.readLine() != null) {
			size++;
		}
	}
	
	public static void main(String[] args) {
		Stopwatch sw = new Stopwatch();
		String fileName = "constants-3200.csv";
		findArraySize(fileName);
		initArray(fileName);
		
		Arrays.sort(doubles);
		int count = 0;
		for(int i = 0; i < doubles.length; i++) {
			for(int j = 0; j < doubles.length; j++) {
				for(int k = 0; k < doubles.length; k++) {
					double a = doubles[i];
					double b = doubles[j];
					double c = doubles[k];
					double d = 0-a-b-c;
					if(Arrays.binarySearch(doubles, d) >= 0) {
						count++;
					}
				}
			}
		}
		System.out.println("There are " + doubles.length + " elements.");
		System.out.println("There are " + count + " four-sums.");
		System.out.println("Time taken is: " + sw.elapsedTime() + " seconds.");
	}
}